package 地区数据;

import Moudle.Excel_class;
import Moudle.Excel_地区数据;
import Moudle.Item;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ToExcel {
    static Logger logger = LoggerFactory.getLogger(ToExcel.class);


    static void toExcel(List<Excel_地区数据> excel_classes, Item item, List<String> zb_list, String FENSHENG) throws IOException, InterruptedException {
        //写入Excel
        Workbook workbook = new SXSSFWorkbook(10000);
        Sheet sheet = workbook.createSheet();

        Row row = sheet.createRow(0);
        int index = 0;
        row.createCell(index++).setCellValue("地区");
        row.createCell(index++).setCellValue("时间");
        for (String str : zb_list)
            row.createCell(index++).setCellValue(str);

        int rowNum = 1;
        int count = 0;
        int n = zb_list.size() + 2;
        for (Excel_地区数据 excel_class : excel_classes) {
            Row row1 = sheet.createRow(rowNum++);
            count++;

            row1.createCell(0).setCellValue(excel_class.City_name);
            row1.createCell(1).setCellValue(excel_class.Time);

            for (int i = 2; i < n; i++)
                row1.createCell(i).setCellValue(excel_class.list.get(i - 2));
            //System.out.println(excel_class.指标 + "  " + excel_class.时间 + "  " + excel_class.指标);
        }


//        workbook.write(new FileOutputStream("C:\\Users\\jinghe\\192.168.1.173\\高新技术企业\\excel导出\\高新技术企业\\认定.xlsx"));
        Item temp = item;
        List<String> list = new ArrayList<>();
        while (!temp.pid.equals("")) {
            list.add(0, temp.name);
            temp = Main.itemMap.get(temp.pid);
        }
        list.add(0, temp.name);

        String path = "";
        for (String str : list) {
            path = path + "\\" + str;
        }


        String File_path = "D:\\测试临时文件\\国家统计局-" + FENSHENG + "\\";
        //String File_path = "D:\\测试临时文件\\国家统计局-新增表格-" + FENSHENG + "\\";

        File file = new File(File_path + path + ".xls");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        workbook.write(new FileOutputStream(file));


        System.out.println("写去excel完毕*--------------------");
        logger.info(file.getAbsolutePath());
        System.out.println("条目数量：" + count + "\n数据数量：" + excel_classes.size());
    }
}
